<template>
	<view v-if="activity_data != null">
		<view class="text-xl padding bg-white">
			<text class="text-bold text-xxl">Title: {{activity_data.title}}</text>
		</view>
		<view class="flex padding bg-white">
			<!-- <i class='iconfont '>积分&#xe629;</i> -->
			<text class="basis-sm text-grey justify-start iconfont">积分&#xe629; : {{activity_data.scores}}</text>
			<text class="basis-sm text-grey justify-end">发起人: {{activity_data.creator}}</text>
		</view>
		<view class="list-picture bg-white">
			<image v-if="activity_data.imgPath[0]" :src='activity_data.imgPath[0]' mode="heightFix"></image>
		</view>
		<view class="cu-list menu">
			<view class="cu-item">
				<view class="content">
					<text class="padding iconfont text-grey text-xxl">&#xe616;</text>
					<text class="text-grey">{{activity_data.date}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="padding iconfont text-grey text-xxl">&#xe618;</text>
					<text class="text-grey">{{activity_data.starttime}}~~{{activity_data.endtime}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="padding iconfont text-grey text-xxl">&#xe610;</text>
					<text class="text-grey">{{activity_data.place}}</text>
				</view>
			</view>
		</view>
		<view class="cu-bar btn-group foot">
			<button class="cu-btn bg-blue shadow-blur round lg" @click="checkinActivity" v-if="activity_data.date==today">签到</button>
			<button class="cu-btn bg-red shadow-blur round lg" v-else>未到日期</button>
		</view>
	</view>
</template>

<script>
	import check_in from '@/common/changeScores.js'
	
	const app = getApp();
	const db = uniCloud.database()
	
	
	export default {
		data() {
			return {
				activity_data: null,
				userInfo: null,
				today: "2021-01-01"
			}
		},



		methods: {
			onLoad(event) {
				uni.showLoading({
					title: '加载ing'
				})
				this.userInfo = app.globalData.userInfo;
				var today = new Date();
				this.today = today.toISOString().slice(0,10);
				db.collection('activities').doc(event.scene).get().then((res) => {
						uni.setNavigationBarTitle({
							title: '签到 | '+res.result.data[0].title
						})
						this.activity_data = res.result.data[0];
						uni.hideLoading();
					})
			},
			checkinActivity: function(event) {
				uni.showLoading({
					title: '签到ing',
					mask: true
				})
				uniCloud.database().collection('uni-id-scores').where({
					_id: this.userInfo._id,
					scoresId: this.activity_data._id
				}).get()
				.then(res => {
					if (res.result.affectedDocs==0) {
						check_in.scoresChange(this.userInfo, this.activity_data, 1)
						.then(res => {
							uni.hideLoading()
							uni.showToast({
								title: '签到成功'
							})
							
						})
					} else if (res.result.affectedDocs==1) {
						uni.hideLoading()
						uni.showToast({
							title: '您已签到'
						})
					}
				})
				
			}
		}
	}
</script>

<style>
	.cu-bar.foot {
		position: fixed;
		width: 100%;
		bottom: 0;
		z-index: 1024;
		box-shadow: 0 -1upx 6upx rgba(0, 0, 0, 0);
	}
</style>
